<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" charset="UTF-8"
	src="../js/prefixfree.min.js"></script>
<link rel="stylesheet" type="text/css" href="../css/base.css"
	media="all" />
<link rel="stylesheet" type="text/css" href="../css/self.css"
	media="all" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="../viewport" content="width=device-width, initial-scale=1">
<link href="../css/bootstrap.css" rel="stylesheet">
<script src="../js/jquery-1.12.4.min.js"></script>
<script src="../js/bootstrap.js"></script>
<!-- 以上五个是在使用bootstrap时需要导入的 -->
<style type="text/css">
</style>
</head>
<body>
	<table class="table table-striped">
		<tr>
			<td class="active">商品ID</td>
			<td class="active">商品类别</td>
			<td class="active">商品名称</td>
			<td class="active">数量</td>
			<td class="active">价格</td>
			<td class="active">颜色</td>
			<td class="active">版本</td>
			<td class="active">详情</td>
			<td class="active">图片</td>
			<td class="active">操作</td>
		</tr>
		<c:forEach var="good" items="${goods }">
			<tr>
				<td>${good.gid }</td>
				<td>${good.tid }</td>
				<td>${good.tname }</td>
				<td>${good.num }</td>
				<td>${good.price }</td>
				<td>${good.color }</td>
				<td>${good.version }</td>
				<td>${good.message }</td>
				<td><img src="../../${good.pic }" style="width:40px;height:40px;"></td>
				<td><button class="btn btn-default" onclick="del(${good.tid })" type="submit">删除</button>
					<button class="btn btn-default" type="submit" 
					onclick="modify('${good.gid}','${good.tid}','${good.tname}','${good.pic}','${good.num}','${good.price}','${good.color}','${good.version}','${good.message}')">修改</button></td>
			</tr>
		</c:forEach>
	</table>
	<ul class="statusbar">
		<nav aria-label="...">
			<ul class="pager">
				<li><a href="javascript:void(0)" onclick="page(-1)">上一页</a></li>&nbsp;&nbsp;
				<li><a href="javascript:void(0)" onclick="page(1)">下一页</a></li>
			</ul>
		</nav>
	</ul>
	
	<script type="text/javascript">
		//http://localhost:8080/Xiaomi/admin/back/goods.jsp#type=0&page=2
		function modify(gid,tid,gname,pic,num,price,color,version,message) {
			sessionStorage.setItem("modifyGood_gid",gid);
			sessionStorage.setItem("modifyGood_tid",tid);
			sessionStorage.setItem("modifyGood_gname",gname);
			sessionStorage.setItem("good_pic",pic);
			sessionStorage.setItem("modifyGood_num",num);
			sessionStorage.setItem("modifyGood_price",price);
			sessionStorage.setItem("modifyGood_color",color);
			sessionStorage.setItem("modifyGood_version",version);
			sessionStorage.setItem("modifyGood_message",message);
			var page = location.href.split("page=")[1];
			sessionStorage.setItem("modifyGood_page",page);
			$.post("../../admin_type",{
				op:'getAllTypes'
			},function(data){
				if(data<0){
					alert('资源加载失败');
				}
				location.href = "goodModify.jsp";
			},"text");
		}
		function del(gid){
			if(gid==null || gid<0){
				return;
			}
			$.post("../../admin_good",{
				op:'good_del',
				gid:gid
			},function(data){
				if(data>0){
					alert("删除成功!重新加载!");
					$.post("../../admin_good",{
						op:'getGoodsbyPage'
					},function(data){
						location.reload();
					},"json");
				}else{
					alert("删除失败!");
				}
			});
		}
		function page(p){
			var locat = location.href.split("back/")[1];
			var page = locat.split("page=")[1];
			
			var newPage = parseInt(page) + parseInt(p);
			console.log("new"+newPage);
			if(newPage<=0){
				newPage = 1;
			}
			$.post("../../admin_good",{
				op:'getGoodsbyPage',
				page:newPage
			},function(data){
				console.log("page:"+data);
				if(data>0){
					var href = "http://localhost:8080/Xiaomi/admin/back/goods.jsp#type=0&page="+data;
					console.log(href);
					location.href=href;
					location.reload();
				}
			});
		}

	</script>
</body>